package rsj.api.test;

import com.rlyy.aibase.utils.base.Box;
import com.rlyy.aibase.utils.base.MapNode;
import common.util.Assert;
import common.util.BoxUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import rsj.api.user.BaseLink;
import rsj.context.ApplicationContextUtil;
import rsj.dao.TestLogDao;
import rsj.sql.test.TestLogServiceDao;
import rsj.utils.RedisUtils;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 获取文件解析结果
 *
 * @author yuanqinglong
 * @since 2021/3/17 9:47
 */
@RestController
public class GetFileParseResultController extends BaseLink {

    private static final TestLogDao TEST_LOG_DAO = TestLogDao.getInstance();

    private static final Map<String, String> RESULT = new HashMap<>();
    public static final String SUCCESS = "2";

    static {
        RESULT.put("0", "等待中");
        RESULT.put("1", "开始任务");
        RESULT.put("2", "成功");
        RESULT.put("98", "任务失败，解析失败，上传原文件");
        RESULT.put("99", "任务失败，调用失败，无任何文件上传");
    }


    @Override
    @RequestMapping(value = "rsj_data_get_upload_result")
    public Box executeLink(Box box) throws SQLException {
        String tid = box.getNode("$tid");
        MapNode data = box.getMapNode("data");
        String logId = data.getValue("log_id");
        if (StringUtils.isEmpty(logId)) {
            return BoxUtil.errorBox(box, "参数不足");
        }
        MapNode info = TEST_LOG_DAO.getInfoByLogId(logId);
        Assert.isNotTrue(info.size() == 0, "此记录不存在");
        String logResult = info.getValue("Log_Result");
        if (Objects.equals(logResult, SUCCESS)) {
            return BoxUtil.successBox(box, "成功");
        }
        // 从redis获取解析结果
        String redisResult = this.queryRedisResult(logId, tid);
        this.deleteRedisResult(logId, tid);
        if (StringUtils.isEmpty(redisResult)) {
            return BoxUtil.errorBox(box, "解析失败");
        }
        // 修改测试数据结果
        this.updateTestDataResult(logId, redisResult);
        // 解析成功、返回成功
        if (Objects.equals(redisResult, SUCCESS)) {
            return BoxUtil.successBox(box, "成功");
        }

        // 解析失败、返回失败原因
        String resultOrDefault = RESULT.getOrDefault(redisResult, "");
        return BoxUtil.errorBox(box, resultOrDefault);
    }


    /**
     * 查询此次数据测试解析的结构状态
     *
     * @param logId 日志id
     * @param tid   tid
     * @return 状态
     * 0：等待中
     * 1：开始任务
     * 2：任务完成，成功
     * 98：任务失败，解析失败，上传原文件
     * 99：任务失败，调用失败，无任何文件上传
     */
    private String queryRedisResult(String logId, String tid) {
        String taskName = "rsjmq:" + logId;
        return RedisUtils.getString(taskName);
    }

    /**
     * 删除redis结果
     *
     * @param logId 日志id
     * @param tid   tid
     */
    private void deleteRedisResult(String logId, String tid) {
        String taskName = "rsjmq:" + logId;
        RedisUtils.del(taskName);
    }


    /**
     * 更新解析结果到数据库
     *
     * @param logId       日志id
     * @param redisResult 解析结果
     * @throws SQLException 异常
     */
    private void updateTestDataResult(String logId, String redisResult) throws SQLException {
        //Connection conn = PostgresqlConn.getConnection();
        //TEST_LOG_DAO.updateTestLogResult(conn, logId, redisResult);
        TestLogServiceDao serviceDao = ApplicationContextUtil.getBean(TestLogServiceDao.class);
        serviceDao.updateTestLogResult(logId, redisResult);
        //Dbink.close(conn);
    }
}
